using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models;

namespace JlPay.Sdk.Models.Openmerch.Device
{
    /// <summary>
    /// 终端查询响应
    /// @author liaochangyi
    /// @since 2025-01-25
    /// </summary>
    public class DeviceQueryResponse : StandardResponse
    {
        /// <summary>
        /// 设备列表
        /// </summary>
        [JsonPropertyName("devices")]
        public List<DeviceInfo>? Devices { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DeviceQueryResponse()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="responseData">响应数据</param>
        public DeviceQueryResponse(Dictionary<string, object> responseData)
        {
            // 处理基础响应字段
            if (responseData.TryGetValue("ret_code", out var retCode))
            {
                RetCode = retCode?.ToString();
            }
            if (responseData.TryGetValue("ret_msg", out var retMsg))
            {
                RetMsg = retMsg?.ToString();
            }

            // 处理设备列表
            if (responseData.TryGetValue("devices", out var devicesData) && devicesData != null)
            {
                if (devicesData is List<object> deviceList)
                {
                    Devices = new List<DeviceInfo>();
                    foreach (var device in deviceList)
                    {
                        if (device is Dictionary<string, object> deviceDict)
                        {
                            Devices.Add(new DeviceInfo(deviceDict));
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 重写HandleResponse方法，确保响应报文格式完全符合预期
        /// </summary>
        /// <param name="operationName">操作名称</param>
        /// <param name="config">配置对象</param>
        public new void HandleResponse(string operationName, Config? config = null)
        {
            if (IsSuccess())
            {
                Console.WriteLine($"{operationName}成功！");
                
                // 手动构建响应JSON，确保字段名称和结构完全符合预期
                var responseData = new Dictionary<string, object>
                {
                    ["ret_code"] = RetCode ?? "",
                    ["ret_msg"] = RetMsg ?? ""
                };

                // 转换设备列表
                if (Devices != null)
                {
                    var deviceList = new List<Dictionary<string, object?>>();
                    foreach (var device in Devices)
                    {
                        deviceList.Add(device.ToDictionary());
                    }
                    responseData["devices"] = deviceList;
                }
                else
                {
                    responseData["devices"] = new List<Dictionary<string, object?>>();
                }
                
                Console.WriteLine("响应报文：" + JsonSerializer.Serialize(responseData, new JsonSerializerOptions 
                { 
                    WriteIndented = true,
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                }));
            }
            else
            {
                Console.WriteLine($"{operationName}失败: {RetCode} - {RetMsg}");
            }
        }
    }
} 